package spring.JUNIT;

import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class UnitTestBase {

	private String springXmlpath;

	private FileSystemXmlApplicationContext context;

	private FileSystemXmlApplicationContext context1;

	public UnitTestBase(String springXmlpath) {
		super();
		this.setSpringXmlpath(springXmlpath);
	}

	@Before
	public void before() {
		if ("".equals(springXmlpath) || null == springXmlpath) {
			springXmlpath = "D:/spring/spring-*.xml";
		}

		try {
			context = new FileSystemXmlApplicationContext(springXmlpath);
			context.start();
		} catch (BeansException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@After
	public void after() {
		context.destroy();

	}

	@SuppressWarnings("unchecked")
	protected <T extends Object> T getBean(String beanId) {
		return (T) context.getBean(beanId);
	}

	protected <T extends Object> T getBean(Class<T> clazz) {
		return context.getBean(clazz);
	}

	public String getSpringXmlpath() {
		return springXmlpath;
	}

	public void setSpringXmlpath(String springXmlpath) {
		this.springXmlpath = springXmlpath;
	}

}
